package com.zsgeek.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zsgeek.db.DBUtils;
import com.zsgeek.db.ResultSetHandler;
import com.zsgeek.entity.Task_review;

public class Task_reviewDao {

	/**
	 * 添加
	 * 
	 * @param task_review
	 * @return
	 */
	public static boolean add(Task_review task_review) {
		String sql = "INSERT INTO task_review(order_no,user_id,review_type,review_star,"
				+ "review_title,review_content,created_time) VALUES (?,?,?,?,?,?,?)";
		return DBUtils.executeUpdate(sql, task_review.getOrder_no(),
				task_review.getUser_id(), task_review.getReview_type(),
				task_review.getReview_star(), task_review.getReview_title(),
				task_review.getReview_content(), task_review.getCreated_time()) > 0;
	}
	

	/**
	 * 修改
	 * 
	 * @param task_review
	 * @return
	 */
	public static boolean update(Task_review task_review) {
		String sql = "UPDATE task_review SET order_no=?,user_id=?,review_type=?,review_star=?,"
				+ "review_title=?,review_content=?,created_time=? WHERE id=?";
		return DBUtils.executeUpdate(sql, task_review.getOrder_no(),
				task_review.getUser_id(), task_review.getReview_type(),
				task_review.getReview_star(), task_review.getReview_title(),
				task_review.getReview_content(), task_review.getCreated_time(),
				task_review.getId()) > 0;
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	public static boolean delete(int id) {
		String sql = "DELETE FROM task_review WHERE id = ?";
		return DBUtils.executeUpdate(sql, id) > 0;
	}

	/**
	 * 查询单个
	 * 
	 * @param id
	 * @return
	 */
	public static Task_review getReview(int id) {
		String sql = "SELECT order_no,user_id,review_type,review_star,"
				+ "review_title,review_content,created_time,id FROM task_review WHERE id=?";
		return (Task_review) DBUtils.executeQuery(sql, new ResultSetHandler() {
			public Object doHandler(ResultSet rs) throws SQLException {
				if (rs.next()) {
					Task_review task_review = new Task_review();
					task_review.setOrder_no(rs.getString(1));
					task_review.setUser_id(rs.getString(2));
					task_review.setReview_type(rs.getInt(3));
					task_review.setReview_star(rs.getInt(4));
					task_review.setReview_title(rs.getString(5));
					task_review.setReview_content(rs.getString(6));
					task_review.setCreated_time(rs.getString(7));
					task_review.setId(rs.getInt(8));
					return task_review;
				}
				return null;
			}
		}, id);
	}

	/**
	 * 根据订单编号查询多个记录(无需分页)
	 * 
	 * @param orderNo
	 * @param review_type
	 *            1:Review 2: Feedback
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<Task_review> getList(String order_no, int review_type,String date) {

		String sql = "SELECT  id,user_id,review_star,review_title,review_content,"
				+ "created_time,order_no,review_type FROM task_review "
				+ "WHERE `order_no`=? AND `review_type`=? AND `created_time`=?";

		return (List<Task_review>) DBUtils.executeQuery(sql,
				new ResultSetHandler() {
					List<Task_review> list = new ArrayList<Task_review>();

					@Override
					public Object doHandler(ResultSet rs) throws SQLException {
						while (rs.next()) {
							Task_review task_review = new Task_review();
							task_review.setId(rs.getInt(1));
							task_review.setUser_id(rs.getString(2));
							task_review.setReview_star(rs.getInt(3));
							task_review.setReview_title(rs.getString(4));
							task_review.setReview_content(rs.getString(5));
							task_review.setCreated_time(rs.getString(6));
							task_review.setOrder_no(rs.getString(7));
							task_review.setReview_type(rs.getInt(8));
							list.add(task_review);
						}
						rs.close();
						return list;
					}
				}, order_no, review_type,date);
	}
	
	
	


}
